package cn.fl.pbl.controller;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.logger.eum.LogOperEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.pbl.service.IPblBillMService;
import cn.fl.pbl.vo.PblBillMVO;
import com.alibaba.fastjson.JSON;
import fl.pbl.facade.pbl.condition.PblBillMQC;
import fl.pbl.facade.pbl.constant.PblBillMConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description: 垫款贷账单信息 控制层
 */
@Controller
@RequestMapping("/pbl/pblBillM")
public class PblBillMController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(PblBillMController.class);

	/**
	 * PblBillM 服务层BEAN
	 */
	@Autowired
	private IPblBillMService pblBillMService;

	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		BaseLogUtils.newLogger("加载列表页面").setLogOperType(LogOperEnum.REQ).setKeys("toListPage",this.getRequestParameter(request),CurrentThreadContext.getCurrentUserName()).info();
		ModelAndView view = new ModelAndView("pbl/pblBillM/pblBillMList");
		//查询数据字典
        List<KeyValueVO> crTypCdList = this.pblBillMService.searchCdList(PblBillMConstant.PBL_CR_TYP_CD);
        List<KeyValueVO> loanStsCdList = this.pblBillMService.searchCdList(PblBillMConstant.PBL_LOAN_STS_CD);
        //贷款类型
        view.addObject("crTypCdList", JSON.toJSONString(crTypCdList));
        //贷款状态
        view.addObject("loanStsCdList", JSON.toJSONString(loanStsCdList));
		return view;
	}
	
	/**
	 * 到详情页面
	 */
	@RequestMapping(value = "/toPblBillDtlPage.do", method = { RequestMethod.GET})
	public ModelAndView toDetailPage(HttpServletRequest request, HttpServletResponse response) {
		BaseLogUtils.newLogger("加载详情页面").setLogOperType(LogOperEnum.REQ).setKeys("toPblBillDtlPage",this.getRequestParameter(request),CurrentThreadContext.getCurrentUserName()).info();
		String id = request.getParameter("id");
		ModelAndView view = new ModelAndView("pbl/pblBillM/pblBillDtl");
		if(StringUtils.isNotEmpty(id)){
			PblBillMVO pblBillMVO = this.pblBillMService.selectById(Long.valueOf(id));
			if(null!=pblBillMVO){
				view.addObject("pblBillMVO",pblBillMVO);				
			}
			view.addObject("billId", id);
		}		
		return view;
	}

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		BaseLogUtils.newLogger("加载列表页面").setLogOperType(LogOperEnum.REQ).setKeys("initList",this.getRequestParameter(request),CurrentThreadContext.getCurrentUserName()).info();
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(pageSize)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		PblBillMQC qc = new PblBillMQC();
		//设置查询条件？
        String cstNm = request.getParameter("cstNm");
        if (StringUtils.isNotBlank(cstNm)) {
            qc.setCstNm(cstNm);
        }
        String dzId = request.getParameter("dzId");
        if (StringUtils.isNotBlank(dzId)) {
        	if(StringUtils.isNumberAndMiddleLine(dzId.trim())){        		
        		qc.setDzId(Long.valueOf(dzId.trim()));
        	}
        }
        String crCntNo = request.getParameter("crCntNo");
        if(StringUtils.isNotBlank(crCntNo)){
            qc.setCrCntNo(crCntNo);
        }
        String loanBizTypCd = request.getParameter("loanBizTypCd");
        if(StringUtils.isNotBlank(loanBizTypCd)){
            qc.setLoanBizTypCd(loanBizTypCd);
        }
        String loanStsCd = request.getParameter("loanStsCd");
        if(StringUtils.isNotBlank(loanStsCd)){
            qc.setLoanStsCd(loanStsCd);
        }
        String crTypCd = request.getParameter("crTypCd");
        if(StringUtils.isNotBlank(crTypCd)){
            qc.setCrTypCd(crTypCd);
        }
        String paySchNo = request.getParameter("paySchNo");
        if(StringUtils.isNotBlank(paySchNo)){
        	qc.setPaySchNo(paySchNo);
		}
        qc.setIsDel(BaseConstant.IS_YESNO_NO);
        page.setCondition(qc);
		Pager pager = this.pblBillMService.searchListPage(page);
		List<PblBillMVO> list = (List<PblBillMVO>) pager.getResultList();
		ListPageVO<PblBillMVO> listPage = new ListPageVO<PblBillMVO>(list, pager.getTotalCount());
		return listPage;
	}

	/**
	 * 得到一条记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.newLogger("据主键获取对象").setLogOperType(LogOperEnum.REQ).setKeys("getById",this.getRequestParameter(request),CurrentThreadContext.getCurrentUserName()).info();

		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, "操作成功!");
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("主键不能为空!");
			BaseLogUtils.newLogger("获取数据,主键不存在").setLogOperType(LogOperEnum.REQ).setKeys("getById","id="+id,CurrentThreadContext.getCurrentUserName()).info();
			return result;
		}
		
		try {
			PblBillMVO vo = this.pblBillMService.selectById(Long.valueOf(id));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("数据不存在!");
				BaseLogUtils.newLogger("获取数据,数据不存在").setLogOperType(LogOperEnum.REQ).setKeys("getById","id="+id,CurrentThreadContext.getCurrentUserName()).info();
			}
			result.setData(vo);
			BaseLogUtils.newLogger("获取数据成功").setLogOperType(LogOperEnum.REQ).setKeys("getById","id="+id,CurrentThreadContext.getCurrentUserName()).info();
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.newLogger("根据主键获取对象失败").setLogOperType(LogOperEnum.REQ).setKeys("getById","id="+id,CurrentThreadContext.getCurrentUserName()).setThrowable(ex).error(LogExceptionEnum.COMMON_CODE_0002);
		} catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("系统错误,请联系管理员!");
			BaseLogUtils.newLogger("根据主键获取对象失败").setLogOperType(LogOperEnum.REQ).setKeys("getById","id="+id,CurrentThreadContext.getCurrentUserName()).setThrowable(ex).error(LogExceptionEnum.COMMON_CODE_0099);
		}

		return result;
	}
}

